const db = require('../db/index')

/**
 * message_title 消息主题
 * message_category 消息类别
 * message_publish_department 消息发布部门
 * message_publish_name 消息发布者
 * message_receipt_object 消息接收者
 * message_click_number 消息查看数量
 * message_content 消息内容
 * message_publish_time 消息发布时间
 * message_update_time 消息更新时间
 * message_level 消息等级
 * message_status 默认为0 ，1在回收站
 * message_delete_time 消息删除时间
 */


// 发布消息 前端参数: 消息题目 消息分类 消息部门 消息发布者 消息接受者 消息内容 消息等级
exports.publishMessage = (req, res) => {
  const { message_title, message_category, message_publish_department, message_publish_name, message_receipt_object, message_content, message_level } = req.body
  const message_publish_time = new Date()
  const sql = `insert into message set ?`
  db.query(sql, {
    message_title,
    message_category,
    message_publish_department,
    message_publish_name,
    message_receipt_object,
    message_content,
    message_level,
    message_publish_time,
    message_click_number: 0,
    message_status: 0
  }, (err, results) => {
    if (err) return res.cc(err)
    if (results.affectedRows !== 1) return res.cc('发布失败')
    res.send({
      status: 0,
      message: '发布成功',
      department: message_publish_department,
      id: results.insertId
    })
  })
}

// 获取公司公告列表 参数: 无
exports.getCompanyMessageList = (req, res) => {
  const sql = `select * from message where message_category ='公司公告' and message_status = '0' order by message_publish_time desc limit 5`
  db.query(sql, (err, results) => {
    if (err) return res.cc(err)
    res.send({
      status: 0,
      message: '获取公司公告列表成功',
      data: results
    })
  })
}

// 获取公司系统消息列表 参数: 无
exports.getSystemMessageList = (req, res) => {
  const sql = `select * from message where message_category ='系统消息' and message_status = '0' order by message_publish_time desc limit 5`
  db.query(sql, (err, results) => {
    if (err) return res.cc(err)
    res.send({
      status: 0,
      message: '获取公司系统消息列表成功',
      data: results
    })
  })
}

// 编辑公告 
exports.editMessage = (req, res) => {
  const {
    message_title,
    message_content,
    message_publish_department,
    message_publish_name,
    message_receipt_object,
    message_level,
    id
  } = req.body
  const message_update_time = new Date()
  const sql = `update message set ? where id = ?`
  db.query(sql, [{
    message_title,
    message_content,
    message_publish_department,
    message_publish_name,
    message_receipt_object,
    message_level,
    message_update_time
  }, id], (err, results) => {
    if (err) return res.cc(err)
    if (results.affectedRows !== 1) return res.cc('编辑失败')
    res.send({
      status: 0,
      message: '编辑成功',
      id
    })
  })
}


// 根据发布部门获取消息 参数: 发布部门
exports.getMessageByDepartment = (req, res) => {
  const pager = (req.body.pager - 1) * 5
  const { message_publish_department } = req.body
  const sql = `select * from message where message_publish_department = ? and message_category = '公司公告' and message_status = 0 order by message_publish_time limit 5 offset ${pager}`
  db.query(sql, message_publish_department, (err, results) => {
    if (err) return res.cc(err)
    res.send({
      status: 0,
      message: `获取${message_publish_department}发布的第${pager + 1}页公司公告列表成功`,
      data: results
    })
  })
}

// 根据发布部门获取消息总数
exports.getMessageCountByDepartment = (req, res) => {
  const { message_publish_department } = req.body
  const sql = `select count(*) as total from message where message_status = 0 and message_category = '公司公告' and message_publish_department = ?`
  db.query(sql, message_publish_department, (err, results) => {
    if (err) return res.cc(err)
    res.send({
      status: 0,
      message: '根据发布部门获取消息总数',
      data: results[0].total
    })
  })
}

// 根据等级获取消息 参数: 等级
exports.getMessageByLevel = (req, res) => {
  const pager = (req.body.pager - 1) * 5
  const { message_level } = req.body
  const sql = `select * from message where message_level = ? and message_category = '公司公告' and message_status = 0 order by message_publish_time limit 5 offset ${pager}`
  db.query(sql, message_level, (err, results) => {
    if (err) return res.cc(err)
    res.send({
      status: 0,
      message: `获取${message_level}等级的第${pager + 1}页公司公告列表成功`,
      data: results
    })
  })
}

// 根据等级获取消息总数
exports.getMessageCountByLevel = (req, res) => {
  const { message_level } = req.body
  const sql = `select count(*) as total from message where message_status = 0 and message_category = '公司公告' and message_level = ?`
  db.query(sql, message_level, (err, results) => {
    if (err) return res.cc(err)
    res.send({
      status: 0,

      message: '根据等级获取消息总数',
      data: results[0].total
    })
  })
}

// 根据 id 获取消息 参数: id
exports.getMessageById = (req, res) => {
  const { id } = req.body
  const sql = `select * from message where id = ? and message_status = '0' `
  db.query(sql, id, (err, results) => {
    if (err) return res.cc(err)
    res.send({
      status: 0,
      message: '根据 id 获取消息成功',
      data: results
    })
  })
}

// 更新点击量 参数: id, message_click_number
exports.updateClickNum = (req, res) => {
  const { message_click_number, id } = req.body
  let number = message_click_number * 1 + 1
  const sql = `update message set message_click_number = ? where id = ?`
  db.query(sql, [number, id], (err, results) => {
    if (err) return res.cc(err)
    res.send({
      status: 0,
      message: '更新点击量成功'
    })
  })
}

// 初次删除 参数: id
exports.deleteMessageFirst = (req, res) => {
  const message_status = 1
  const message_delete_time = new Date()
  const sql = 'update message set message_status = ? , message_delete_time = ? where id = ?'
  db.query(sql, [message_status, message_delete_time, req.body.id], (err, result) => {
    if (err) return res.cc(err)
    res.send({
      status: 0,
      message: '删除成功'
    })
  })
}

// 获取回收站列表 参数：无
exports.getRecycleBinList = (req, res) => {
  const sql = `select * from message where message_status = 1`
  db.query(sql, (err, results) => {
    if (err) return res.cc(err)
    res.send({
      status: 0,
      message: '获取回收站列表成功',
      data: results
    })
  })
}

// 获取回收站总数 参数：无
exports.getRecycleBinTotal = (req, res) => {
  const sql = `select count(*) as total from message where message_status = 1`
  db.query(sql, (err, results) => {
    if (err) return res.cc(err)
    res.send({
      status: 0,
      message: '获取回收站总数成功',
      data: results[0].total
    })
  })
}

// 回收站换页 参数：pager
exports.getRecycleBinByPager = (req, res) => {
  const pager = (req.body.pager - 1) * 5
  const sql = `select * from message where message_status = 1 order by message_delete_time limit 5 offset ${pager}`
  db.query(sql, (err, results) => {
    if (err) return res.cc(err)
    res.send({
      status: 0,
      message: `获取第${pager + 1}回收站列表成功`,
      data: results
    })
  })
}

// 还原 参数：id
exports.restoreMessage = (req, res) => {
  const sql = 'update message set message_status = 0 where id = ?'
  db.query(sql, req.body.id, (err, result) => {
    if (err) return res.cc(err)
    res.send({
      status: 0,
      message: '还原成功'
    })
  })
}

// 彻底删除 参数：id
exports.deleteMessage = (req, res) => {
  const sql = 'delete from message where id = ?'
  db.query(sql, req.body.id, (err, result) => {
    if (err) return res.cc(err)
    res.send({
      status: 0,
      message: '彻底删除成功'
    })
  })
}

// 获取公司公告总数 参数: 无
exports.getCompanyMessageTotal = (req, res) => {
  const sql = `select count(*) as total from message where message_category = '公司公告' and message_status = 0`
  db.query(sql, (err, results) => {
    if (err) return res.cc(err)
    res.send({
      status: 0,
      message: '获取公司公告总数成功',
      data: results[0].total
    })
  })
}


// 获取系统消息总数 参数: 无
exports.getSystemMessageTotal = (req, res) => {
  const sql = `select count(*) as total from message where message_category = '系统消息' and message_status = 0`
  db.query(sql, (err, results) => {
    if (err) return res.cc(err)
    res.send({
      status: 0,
      message: '获取系统消息总数成功',
      data: results[0].total
    })
  })
}


// 公司公告列表换页 参数: pager
exports.getCompanyMessageByPager = (req, res) => {
  const pager = (req.body.pager - 1) * 5
  const sql = `select * from message where message_category = '公司公告' and message_status = 0 order by message_publish_time limit 5 offset ${pager}`
  db.query(sql, (err, results) => {
    if (err) return res.cc(err)
    res.send({
      status: 0,
      message: `获取第${pager + 1}页公司公告列表成功`,
      data: results
    })
  })
}

// 系统消息列表换页 参数: pager
exports.getSystemMessageByPager = (req, res) => {
  const pager = (req.body.pager - 1) * 5
  const sql = `select * from message where message_category = '系统消息' and message_status = 0 order by message_publish_time limit 5 offset ${pager}`
  db.query(sql, (err, results) => {
    if (err) return res.cc(err)
    res.send({
      status: 0,
      message: `获取第${pager + 1}页系统消息列表成功`,
      data: results
    })
  })
}
